/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * @author anhmantk
 * @date Dec 30, 2011
 */

$(function(){
    loadSubCate('select#showsub_0');
});

function loadSubCate(obj) {
    $(obj).change(function(){
        var id = $(this).val();
        $('#cate_id').val(id);
        $('#cate_name_hidden').val($('option[value="' + id + '"]').html());
        //xoa bo combobox neu da co
        var lv = $(obj).attr('level');
        $('select.showsub').each(function(){
            if ($(this).attr('level') > lv) {
                $(this).parent('div').remove();
            //$(this).prev().remove();
            //$(this).remove();                
            }
        });
        
        
        if (id) {            
            $.ajax({
                type: 'GET',
                url: Drupal.settings.basePath + 'back-end/danhmuc/' + $('#action').val() + '/load-sub-cate',
                data: {
                    cate_id: id,
                    level: lv
                },
                beforeSend: function (jqXHR, settings) {
                    $('span#loadding').css('visibility', 'visible');
                },
                success: function(data, textStatus, jqXHR) {
                    switch(data) {
                        case 'raovat':
                            showButRaoVat();
                            break;
                        case 'hoidap':
                            showButHoiDap();
                            break;
                        case 'sanpham':
                            showButSanPham();
                            break;
                        default:
                            loadSubCateSuccess(data ,id);
                    }                 
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('Có lỗi xẩy ra!');
                },
                complete: function(jqXHR, textStatus) {
                    $('span#loadding').css('visibility', 'hidden');            
                }
            });
        }
    });        
}


function loadSubCateSuccess(data ,id) {
    $('#showcate').append(data);
    loadSubCate('select#showsub_' + id);
    $("#dangsp").attr('disabled',true);
    $('#show_form_post').attr('disabled',true); //an nut Dang tin rao vat
}

function showButSanPham() {
    $("#dangsp").attr('disabled',false);
}

function showButRaoVat() {
    $('#show_form_post').attr('disabled',false);
}

function showButHoiDap() {
    $('#show_form_post').attr('disabled',false);
}
